/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.testis.utils.tree;

import org.testis.orm.classes.T_test_label;

/**
 * 
 * @author selcuk
 */
public class LabelTreeNode extends TreeNode {

    public LabelTreeNode(String name) {
	super(name, null, null);
    }

    public LabelTreeNode(String name, boolean select) {
	super(name, null, null);
	setSelected(select);
    }

    public LabelTreeNode(T_test_label label) {
	super(label != null ? label.getName() : "null", label, null);
    }

    public LabelTreeNode(T_test_label label, boolean select) {
	super(label != null ? label.getName() : "null", label, null);
	setSelected(select);
    }

    @Override
    protected int getChildIndex(TreeNode node) {
	int index = -1;
	LabelTreeNode dNode = (LabelTreeNode) node;
	for (int i = 0; i < getChildrenLinkedList().size(); i++) {
	    LabelTreeNode child = (LabelTreeNode) getChildrenLinkedList()
		    .get(i);
	    if (child.getName().equals(dNode.getName())) {
		index = i;
		break;
	    }
	}
	return index;
    }

    @Override
    protected boolean isContainsChild(TreeNode node) {
	int index = getChildIndex(node);
	if (index == -1) {
	    return false;
	} else {
	    return true;
	}
    }

    @Override
    public void deleteChild(TreeNode node) {
	if (node != null && node.getParent().equals(this)) {
	    LabelTreeNode dNode = (LabelTreeNode) node;
	    for (int i = 0; i < getChildrenLinkedList().size(); i++) {
		LabelTreeNode child = (LabelTreeNode) getChildrenLinkedList()
			.get(i);
		if (child.getName().equals(dNode.getName())) {
		    getChildrenLinkedList().remove(i);
		    break;
		}
	    }
	    node.setParent(null);
	}
    }

    @Override
    public String toString() {
	T_test_label label = (T_test_label) getObject();
	String result = "";
	if (label != null) {
	    result = label.getName();
	} else {
	    result = getName();
	}
	return result;
    }
}
